package com.sise.stuteaintersys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sise.stuteaintersys.bean.base.ModifyResponse;
import com.sise.stuteaintersys.bean.entity.*;
import com.sise.stuteaintersys.bean.vo.*;
import com.sise.stuteaintersys.bean.vo.element.CourseIndexResponseVO;
import com.sise.stuteaintersys.bean.vo.element.ProfessionSortVO;
import com.sise.stuteaintersys.mapper.*;
import com.sise.stuteaintersys.service.StudentCourseService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CourseServiceImpl
 * @Description 返回课程相关的的信息
 * @Date 2018/12/29 22:29
 * @Version 1.0
 **/
@Service
public class StudentCourseServiceImpl implements StudentCourseService {
    @Autowired
    Mapper dozerMapper;
    @Autowired
    ProfessionTypeMapper professionTypeMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    ExamMapper examMapper;
    @Autowired
    ExamCourseMapper examCourseMapper;
    @Autowired
    DownloadInfoMapper downloadInfoMapper;
    @Autowired
    HomeworkMapper homeworkMapper;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    StudentCourseMapper studentCourseMapper;
    @Autowired
    HomeworkResultMapper homeworkResultMapper;
    @Autowired
    ExamResultMapper examResultMapper;

    @Override
    public MainResponseVO responseMain() {
        //获取所有专业分向
        String[] professionList = professionTypeMapper.selectName();
        //开始构建主页返回数据
        MainResponseVO mainResponseVO = new MainResponseVO();
        mainResponseVO.setProfessionList(professionList);
        List<ProfessionSortVO> sortList = new ArrayList<>();
        for (String s : professionList) {
            ProfessionSortVO sort = new ProfessionSortVO();
            sort.setProfessionName(s);
            //通过专业分向查询课程，每个list只有五条记录
            List<Course> list = courseMapper.selectByProfessionName(s);
            sort.setCourseMainResponses(list);
            sortList.add(sort);
        }
        mainResponseVO.setProfessionSortVO(sortList);
        return mainResponseVO;
    }


    @Override
    public CourseMainVO getCourseBody(Integer id) {
        Course course = courseMapper.selectByPrimaryKey(id);
        CourseMainVO courseMainVO = dozerMapper.map(course, CourseMainVO.class);
        return courseMainVO;
    }

    @Override
    public PageInfo<Message> listMessage(Integer pageNum, Integer pageSize, Integer courseId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Message> list = messageMapper.selectAllMessage(courseId);
        return new PageInfo<>(list);
    }

    @Override
    public boolean insertMessage(MessageAddVO messageAddVO) {
        try {
            Message message = dozerMapper.map(messageAddVO, Message.class);
            message.setGmtCreate(new Date());
            message.setQuestionTime(new Date());
            message.setGmtModified(new Date());
            messageMapper.insert(message);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public PageInfo<ExamCourse> listExam(Integer pageNum, Integer pageSize, Integer courseId) {
        PageHelper.startPage(pageNum, pageSize);
        List<ExamCourse> list = examCourseMapper.selectExamByCourseId(courseId);
        return new PageInfo<>(list);
    }

    @Override
    public ExamResultVO listQuestion(String examType, Integer courseId, Integer studentId) {
        ExamResultVO examResultVO = new ExamResultVO();
        ExamResult examResult = examResultMapper.select(courseId, studentId, examType);
        List<Question> list = questionMapper.selectExam(courseId + examType);
        examResultVO.setExamResult(examResult);
        examResultVO.setQuestionList(list);
        return examResultVO;
    }

    @Override
    public ModifyResponse insertExamResult(ExamResult examResult) {
        ModifyResponse modifyResponse = new ModifyResponse();
        examResult.setStatus("");
        int number = examResultMapper.insertSelective(examResult);
        modifyResponse.setNumber(number);
        if (number == 1) {
            modifyResponse.setStatus(true);
            modifyResponse.setMsg("提交成功");
        } else {
            modifyResponse.setStatus(false);
            modifyResponse.setMsg("提交失败");
        }
        return modifyResponse;
    }

    @Override
    public PageInfo<DownloadInfo> listDownloadInfo(Integer pageNum, Integer pageSize, Integer courseId) {
        PageHelper.startPage(pageNum, pageSize);
        List<DownloadInfo> list = downloadInfoMapper.selectAllDownloadInfo(courseId);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Homework> listHomework(Integer pageNum, Integer pageSize, Integer courseId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Homework> list = homeworkMapper.selectAllHomework(courseId);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<HomeworkDateVO> AllHomework(Integer pageNum, Integer pageSize, Integer courseId) {
        PageHelper.startPage(pageNum, pageSize);
        List<String> stringList = homeworkMapper.listHomework(courseId);
        List<HomeworkDateVO> datelist = new ArrayList<>();
        String t;
        for (int i = 0; i < stringList.size(); i++) {
            t = stringList.get(i);
            HomeworkDateVO s = new HomeworkDateVO();
            s.setDate(t);
            datelist.add(s);
        }
        return new PageInfo<>(datelist);
    }

    @Override
    public HomeworkResultVO getHomework(Date date, Integer courseId, Integer studentId) {
        HomeworkResultVO homeworkResultVO = new HomeworkResultVO();
        HomeworkResult homeworkResult = homeworkResultMapper.select(courseId, studentId, date);
        List<Homework> list = homeworkMapper.getHomework(date, courseId);
        homeworkResultVO.setHomeworkResult(homeworkResult);
        homeworkResultVO.setHomeworkList(list);
        list = homeworkMapper.getHomework(date, courseId);
        return homeworkResultVO;
    }

    @Override
    public ModifyResponse commitHomework(HomeworkResult homeworkResult) {
        ModifyResponse modifyResponse = new ModifyResponse();
        System.out.println(homeworkResult.toString());
        try {
            int number = homeworkResultMapper.insertSelective(homeworkResult);
            modifyResponse.setNumber(number);
            if (number == 0) {
                modifyResponse.setStatus(false);
                modifyResponse.setMsg("提交失败");
            } else {
                modifyResponse.setMsg("提交成功");
                modifyResponse.setStatus(true);
            }
            return modifyResponse;
        } catch (Exception e) {
            modifyResponse.setStatus(false);
            modifyResponse.setMsg("提交失败");
            return modifyResponse;
        }
    }

    @Override
    public PageInfo<Course> finishedCourse(Integer pageNum, Integer pageSize, Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> list = courseMapper.finishedCourse(id);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<CourseIndexResponseVO> findCourseByCourseName(Integer pageNum, Integer pageSize, String courseName) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseIndexResponseVO> list = courseMapper.selectLikeCourse(courseName);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<CourseIndexResponseVO> findCourseByTeacherName(Integer pageNum, Integer pageSize, String teacherName) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseIndexResponseVO> list = courseMapper.selectFuzzyTeacher(teacherName);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<CourseIndexResponseVO> listProfessionFind(Integer pageNum, Integer pageSize, String professionName) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseIndexResponseVO> list = courseMapper.selectCourseByProfession(professionName);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<CourseIndexResponseVO> listCourseTypeFind(Integer pageNum, Integer pageSize, String courseType) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseIndexResponseVO> list = courseMapper.selectCourseByType(courseType);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Course> listCourse(Integer pageNum, Integer pageSize, Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> list = courseMapper.listCourse(id);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Course> selectCourse(Integer pageNum, Integer pageSize, Course course) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> list = courseMapper.selectCourse(course);
        return new PageInfo<>(list);
    }

    @Override
    public boolean joinCourse(StudentCourse studentCourse) {
        studentCourse.setGmtCreate(new Date());
        studentCourse.setGmtModified(new Date());
        int number = studentCourseMapper.insert(studentCourse);
        if (number == 1) {
            return true;
        } else {
            return false;
        }
    }
}
